package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.domain.SysOperateLog;
import com.grape.service.ISysOperateLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 操作日志表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("operateLogs")
@Api(tags = "操作日志接口")
@AllArgsConstructor
public class SysOperateLogController extends BaseController<SysOperateLog> {

    private final ISysOperateLogService sysOperateLogService;

    @GetMapping
    @ApiOperation(value = "操作日志列表")
    public R<?> listPage(BaseVo baseVo) {
        Page<SysOperateLog> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        List<SysOperateLog> sysPermissionList = sysOperateLogService.listData(baseVo);
        PageInfo<SysOperateLog> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(sysPermissionList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @PutMapping
    @ApiOperation(value = "操作日志修改")
    public R<?> update(@Validated @RequestBody SysOperateLog sysOperateLog){
        if (!Strings.isNotBlank(sysOperateLog.getId())){
            return updateNotFound(sysOperateLog, null);
        }
        sysOperateLogService.updateData(sysOperateLog);
        return updateSuccess(null, null);
    }


    @PostMapping
    @ApiOperation(value = "添加操作日志")
    public R<?> insert(@Validated @RequestBody SysOperateLog sysOperateLog){
        return insertSuccess(sysOperateLogService.insertData(sysOperateLog), null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除操作日志，可批量多个id逗号拼接")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids){
        int number = sysOperateLogService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @GetMapping("detail")
    @ApiOperation(value = "操作日志详情")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id){
        SysOperateLog sysOperateLog = sysOperateLogService.detail(id);
        if (sysOperateLog != null) {
            return selectSuccess(sysOperateLog, id);
        }
        return selectNotFound(id, null);
    }
}

